<template>
  <el-dialog
    v-el-drag-dialog
    :title="dataForm.activityId===0?'新增活动':'修改活动'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动标题" prop="activityTitle">
      <el-input v-model="dataForm.activityTitle" placeholder="活动标题会在用户端页面展示,活动标题6个字符内" maxlength="6"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="time">
      <el-date-picker
        :editable="false"
        v-model="dataForm.time"
        type="daterange"
        value-format = "yyyy-MM-dd HH:mm:ss"
        range-separator="-"
        start-placeholder="活动开始时间"
        end-placeholder="活动结束时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="备注" prop="publishRemark">
      <el-input v-model="dataForm.publishRemark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          activityId: 0,
          activityNo: '',
          activityTitle: '',
          beginTime: '',
          endTime: '',
          publishRemark: '',
          time: new Date()
        },
        dataRule: {
          activityTitle: [
            { required: true, message: '活动标题不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.activityId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.activityId) {
            this.$http({
              url: this.$http.adornUrl(`/activity/spike/getActivityById/${this.dataForm.activityId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.activityNo = data.data.activityNo
                this.dataForm.activityTitle = data.data.activityTitle
                this.dataForm.publishRemark = data.data.publishRemark
                this.dataForm.time = [data.data.beginTime, data.data.endTime]
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/spike/${!this.dataForm.activityId ? 'add' : 'modify'}`),
              method: 'post',
              data: this.$http.adornData({
                'activityId': this.dataForm.activityId || undefined,
                'activityNo': this.dataForm.activityNo,
                'activityTitle': this.dataForm.activityTitle,
                'beginTime': this.dataForm.time[0],
                'endTime': this.dataForm.time[1],
                'publishRemark': this.dataForm.publishRemark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
